package com.sansecy.monitor.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sansecy.monitor.R;
import com.sansecy.monitor.see.LogBean;
import com.sansecy.monitor.utils.QrcodeImageUtils;
import com.sansecy.monitor.utils.Utils;

/**
 * @Description 包含请求信息的视图
 * @Date 2019-08-08 10:44
 * @Created by caoyu
 */
public class RequestView extends ScrollView implements View.OnClickListener {

    private TextView tvParams;
    private TextView tvFullUrl;
    private TextView tvContentType;
    private TextView tvHost;
    private TextView tvMethod;
    private TextView tvUrl;
    private TextView tvPath;
    private ImageView ivQrCode;

    public RequestView(Context context) {
        this(context, null);
    }

    public RequestView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_request, this, true);
        init();
    }

    private void init() {
        tvParams = findViewById(R.id.tv_params);
        tvParams.setOnClickListener(this);
        tvFullUrl = findViewById(R.id.tv_full_url);
        tvFullUrl.setOnClickListener(this);
        tvContentType = findViewById(R.id.tv_content_type);
        tvContentType.setOnClickListener(this);
        tvHost = findViewById(R.id.tv_host);
        tvHost.setOnClickListener(this);
        tvMethod = findViewById(R.id.tv_method);
        tvMethod.setOnClickListener(this);
        tvUrl = findViewById(R.id.tv_url);
        tvUrl.setOnClickListener(this);
        tvPath = findViewById(R.id.tv_path);
        tvPath.setOnClickListener(this);
        ivQrCode = findViewById(R.id.iv_qr_code);
    }

    public void bindData(LogBean logBean) {
        tvFullUrl.setText(logBean.getFullUrl());
        tvParams.setText(logBean.getDecryptParams());
        tvContentType.setText(logBean.getRequestMediaType());

        tvHost.setText(logBean.getHost());
        tvMethod.setText(logBean.getMethod());
        tvUrl.setText(logBean.getUrl());
        tvPath.setText(logBean.getPath());
        Bitmap bitmap = QrcodeImageUtils.createQRImage(logBean.getFullUrl() + "&platform=android", 200, 200);
        ivQrCode.setImageBitmap(bitmap);
    }




    @Override
    public void onClick(View v) {
        Utils.copyToClipboard(v);
    }
}
